package com.example.board.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.board.mapper.OperatorAnalysisMapper;
import com.example.board.pojo.dto.UserRecordDto;
import com.example.board.pojo.vo.IndustryShareDetailVo;
import com.example.board.pojo.vo.IndustryShareVo;
import com.example.board.service.OperatorAnalysisService;
import com.example.board.util.DateQuery;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class OperatorAnalysisServiceImpl implements OperatorAnalysisService {

    private  final OperatorAnalysisMapper operatorAnalysisMapper;

    @Override
    public String getAllDetail() {
        //获取三个月 第一天
        String mothStartLast = DateQuery.getMothStartLast2();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startMoth = LocalDateTime.parse(mothStartLast, df);
        //查询三个月的 成交客户数量
        return operatorAnalysisMapper.findAllDetail3(startMoth);
    }

    @Override
    public List<IndustryShareVo> getIndustryShareMax(String province, String region) {
        if (StringUtils.isEmpty(province) && StringUtils.isEmpty(region)){
            province = "广东省";
            region = "深圳市";
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<IndustryShareVo> industryShareVos = new ArrayList<>();
        //当月
        //当月
        LocalDateTime startMoth = LocalDateTime.parse(DateQuery.getMothStart(), df);
        LocalDateTime endMoth = LocalDateTime.parse(DateQuery.getMothEnd(), df);
        String mothNow = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])) + "月";
        IndustryShareVo industryShareVo = getIndustryShareVo(startMoth, endMoth, province, region, mothNow);
        industryShareVos.add(industryShareVo);

        //上月 第一天和最后一天
        //上月 第一天和最后一天
        LocalDateTime startMothLsat = LocalDateTime.parse(DateQuery.getMothStartLast(), df);
        LocalDateTime endMothLast = LocalDateTime.parse(DateQuery.getMothEndLast(), df);
        String mothLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])-1) + "月";
        IndustryShareVo industryShareVoLast = getIndustryShareVo(startMothLsat, endMothLast, province, region, mothLast);
        industryShareVos.add(industryShareVoLast);

        //这上上月 第一天和最后一天
        LocalDateTime startMothLastLast = LocalDateTime.parse(DateQuery.getMothStartLast2(), df);
        LocalDateTime endMothLastLast = LocalDateTime.parse(DateQuery.getMothEndLast2(), df);
        String mothLastLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])-2) + "月";
        IndustryShareVo industryShareVoLastLast = getIndustryShareVo(startMothLastLast, endMothLastLast, province, region, mothLastLast);
        industryShareVos.add(industryShareVoLastLast);

        return industryShareVos;
    }

    public IndustryShareVo getIndustryShareVo(LocalDateTime startMoth,LocalDateTime endMoth,String province,String  region,String moth){

        //总订单
        List<IndustryShareVo> allTotal = operatorAnalysisMapper.findAllDealNow(startMoth,endMoth,province,region);
        IndustryShareVo industryShareVo = new IndustryShareVo();

        if (StringUtils.isNotEmpty(region)){
            industryShareVo.setArea(region);
        }else {
            industryShareVo.setArea(province);
        }
        industryShareVo.setCustomerNumber(0);
        industryShareVo.setDate(moth);
        industryShareVo.setTypeName("暂无");
        industryShareVo.setPercent(new BigDecimal(0));

        if (CollectionUtils.isNotEmpty(allTotal)){

            //最多
            String typeName = allTotal.get(0).getTypeName();
            Integer customerNumber = allTotal.get(0).getCustomerNumber();
            //总订单
            int sum = allTotal.stream().mapToInt(IndustryShareVo::getCustomerNumber).sum();

            if (StringUtils.isNotEmpty(region)){
                industryShareVo.setArea(region);
            }else {
                industryShareVo.setArea(province);
            }

            industryShareVo.setCustomerNumber(customerNumber);
            industryShareVo.setDate(moth);
            industryShareVo.setTypeName(typeName);
            industryShareVo.setPercent(new BigDecimal(customerNumber).divide(new BigDecimal(sum),2, RoundingMode.HALF_UP));

        }
        return industryShareVo;
    }

    @Override
    public List<IndustryShareDetailVo> getIndustryShareDetail(String province, String region) {
        if (StringUtils.isEmpty(province) && StringUtils.isEmpty(region)){
            province = "广东省";
            region = "深圳市";
        }

        List<IndustryShareDetailVo> industryShareDetailVos = new ArrayList<>();
        //得到餐饮类别 所属城市下
        List<String> allTypeName = operatorAnalysisMapper.findAllTypeName(province,region);
        //三个月拉出来
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        //当月
        LocalDateTime startMoth = LocalDateTime.parse(DateQuery.getMothStart(), df);
        LocalDateTime endMoth = LocalDateTime.parse(DateQuery.getMothEnd(), df);
        String mothNow = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])) + "月";

        //上月 第一天和最后一天
        LocalDateTime startMothLsat = LocalDateTime.parse(DateQuery.getMothStartLast(), df);
        LocalDateTime endMothLast = LocalDateTime.parse(DateQuery.getMothEndLast(), df);
        String mothLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])-1) + "月";

        //这上上月 第一天和最后一天
        LocalDateTime startMothLastLast = LocalDateTime.parse(DateQuery.getMothStartLast2(), df);
        LocalDateTime endMothLastLast = LocalDateTime.parse(DateQuery.getMothEndLast2(), df);
        String mothLastLast = (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1])-2) + "月";

        //查询所有类别成交客户数量 当月 上月 上上月
        List<UserRecordDto> typeNow = operatorAnalysisMapper.findTypeNow(startMoth, endMoth, province, region, allTypeName);
        List<UserRecordDto> typeNowLast = operatorAnalysisMapper.findTypeNow(startMothLsat, endMothLast, province, region, allTypeName);
        List<UserRecordDto> typeNowLastLats = operatorAnalysisMapper.findTypeNow(startMothLastLast, endMothLastLast, province, region, allTypeName);

        //查询城市成交客户数量
        Integer typeNowTotal = operatorAnalysisMapper.findTypeAllNow(startMoth, endMoth, province, region);
        Integer typeNowTotalLast = operatorAnalysisMapper.findTypeAllNow(startMothLsat, endMothLast, province, region);
        Integer typeNowTotalLastLast = operatorAnalysisMapper.findTypeAllNow(startMothLastLast, endMothLastLast, province, region);


        for (String typeClass : allTypeName) {
            IndustryShareDetailVo industryShareDetailVo = new IndustryShareDetailVo();
            industryShareDetailVo.setCustomerMonth1(mothLastLast);
            industryShareDetailVo.setCustomerMonth2(mothLast);
            industryShareDetailVo.setCustomerMonth3(mothNow);

            industryShareDetailVo.setTradeCustomerNumber1(0);
            industryShareDetailVo.setTradeCustomerNumber2(0);
            industryShareDetailVo.setTradeCustomerNumber3(0);

            industryShareDetailVo.setTradeCustomerPercent1(new BigDecimal(0));
            industryShareDetailVo.setTradeCustomerPercent2(new BigDecimal(0));
            industryShareDetailVo.setTradeCustomerPercent3(new BigDecimal(0));

            industryShareDetailVo.setTradeCustomerPercentTotal(new BigDecimal(0));
            industryShareDetailVo.setTypeAndMonth1(typeClass+"/"+mothLastLast);
            industryShareDetailVo.setCustomerType(typeClass);
            industryShareDetailVo.setTradeCustomerNumberTotal(0);
//            industryShareDetailVo.setId();


            if (CollectionUtils.isNotEmpty(typeNow)){

                for (UserRecordDto userRecordDto : typeNow) {
                    if (userRecordDto.getShopClass().equals(typeClass)){
                        industryShareDetailVo.setTradeCustomerNumber3(userRecordDto.getCustomerNumber());
                        if (typeNowTotal != null && typeNowTotal != 0){
                            industryShareDetailVo.setTradeCustomerPercent3(new BigDecimal(userRecordDto.getCustomerNumber()).divide(new BigDecimal(typeNowTotal),2, RoundingMode.HALF_UP));
                        }
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(typeNowLast)){
                for (UserRecordDto userRecordDto : typeNowLast) {
                    if (userRecordDto.getShopClass().equals(typeClass)){
                        industryShareDetailVo.setTradeCustomerNumber2(userRecordDto.getCustomerNumber());
                        if (typeNowTotalLast != null && typeNowTotalLast != 0){
                            industryShareDetailVo.setTradeCustomerPercent2(new BigDecimal(userRecordDto.getCustomerNumber()).divide(new BigDecimal(typeNowTotalLast),2, RoundingMode.HALF_UP));
                        }
                    }
                }
            }

            if (CollectionUtils.isNotEmpty(typeNowLastLats)){
                for (UserRecordDto userRecordDto : typeNowLastLats) {
                    if (userRecordDto.getShopClass().equals(typeClass)){
                        industryShareDetailVo.setTradeCustomerNumber1(userRecordDto.getCustomerNumber());
                        if (typeNowTotalLastLast != null && typeNowTotalLastLast != 0){
                            industryShareDetailVo.setTradeCustomerPercent1(new BigDecimal(userRecordDto.getCustomerNumber()).divide(new BigDecimal(typeNowTotalLastLast),2, RoundingMode.HALF_UP));
                        }
                    }
                }
            }


            int total = industryShareDetailVo.getTradeCustomerNumber1()+
                    industryShareDetailVo.getTradeCustomerNumber2()+
                    industryShareDetailVo.getTradeCustomerNumber3();

            int totalAll = typeNowTotal+typeNowTotalLast+typeNowTotalLastLast;

            industryShareDetailVo.setTradeCustomerNumberTotal(total);

            if (totalAll != 0){
                industryShareDetailVo.setTradeCustomerPercentTotal(new BigDecimal(total).divide(new BigDecimal(totalAll),2, RoundingMode.HALF_UP));
            }

            industryShareDetailVos.add(industryShareDetailVo);

        }
        List<IndustryShareDetailVo> collect = industryShareDetailVos.stream().sorted(Comparator
                .comparing(IndustryShareDetailVo::getTradeCustomerNumberTotal).reversed()).collect(Collectors.toList());
        int i = 1;
        for (IndustryShareDetailVo industryShareDetailVo : collect) {
            industryShareDetailVo.setId(i);
            i++;
        }
        return collect;
    }


}
